package com.lingjtx.order.controller;

import com.alibaba.fastjson2.JSONObject;
import com.lingjtx.common.core.ResponseResult;
import com.lingjtx.common.core.annotation.ApiSignature;
import com.lingjtx.common.core.constant.ResponseTip;
import com.lingjtx.common.security.annotation.NonRepeatable;
import com.lingjtx.order.po.GoodsSettlementPo;
import com.lingjtx.order.po.OrderModelPo;
import com.lingjtx.order.po.SubmitOrderPo;
import com.lingjtx.order.service.OrderService;
import com.lingjtx.order.vo.GoodsSettlementVo;
import com.lingjtx.order.vo.OrderDetailVo;
import com.lingjtx.order.vo.OrderListVo;
import com.lingjtx.order.vo.SettlementVo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pay")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 购物车去结算
     */
    @PostMapping("/cart_settlement")
    public ResponseResult<SettlementVo> cartSettlement() {
        return ResponseResult.createBySuccess(orderService.cartSettlement());
    }

    /**
     * 单个商品直接结算
     */
    @PostMapping("/goods_settlement")
    public ResponseResult<GoodsSettlementVo> goodsSettlement(@Valid @RequestBody GoodsSettlementPo model) {
        return ResponseResult.createBySuccess(orderService.goodsSettlement(model));
    }

    /**
     * 提交订单
     */
    @ApiSignature
    @NonRepeatable
    @PostMapping("/submit")
    public ResponseResult<Object> submit(@Valid @RequestBody SubmitOrderPo model) {
        return ResponseResult.createBySuccess(orderService.submit(model));
    }

    /**
     * 订单列表
     */
    @GetMapping("/list")
    public ResponseResult<List<OrderListVo>> list(Integer orderStatus,
                                                  @RequestParam(name = "page", defaultValue = "1") int page,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return ResponseResult.createBySuccess(orderService.list(orderStatus, page, pageSize));
    }

    /**
     * 删除订单
     */
    @ApiSignature
    @PostMapping("/delete/{orderNo}")
    public ResponseResult<Void> delete(@PathVariable("orderNo") String orderNo) {
        boolean result = orderService.delete(orderNo);
        if (result) {
            return ResponseResult.createBySuccess(ResponseTip.RESPONSE_TIP_DELETE);
        }
        return ResponseResult.createByError(ResponseTip.RESPONSE_TIP_OPERATION_ERROR);
    }

    /**
     * 去支付（待支付订单）
     */
    @ApiSignature
    @PostMapping("/to_pay")
    public ResponseResult<Object> toPay(@Valid @RequestBody OrderModelPo model) {
        Object result = orderService.toPay(model.getOrderNo());
        if (result == null) {
            return ResponseResult.createByError("订单已失效");
        }
        return ResponseResult.createBySuccess(result);
    }

    /**
     * 订单详情
     */
    @GetMapping("/detail/{orderNo}")
    public ResponseResult<OrderDetailVo> detail(@PathVariable("orderNo") String orderNo) {
        return ResponseResult.createBySuccess(orderService.detail(orderNo));
    }

    /**
     * 修改订单地址
     */
    @PostMapping("/update_order_address")
    public ResponseResult<Void> updateOrderAddress(@Valid @RequestBody OrderModelPo model) {
        boolean result = orderService.updateOrderAddress(model.getOrderNo(), model.getAddressId());
        if (result) {
            return ResponseResult.createBySuccess("修改成功");
        }
        return ResponseResult.createByError("订单已发货，不能修改地址");
    }

    /**
     * 确认收货
     */
    @ApiSignature
    @NonRepeatable
    @PostMapping("/confirm_receipt")
    public ResponseResult<Void> confirmReceipt(@Valid @RequestBody OrderModelPo model) {
        boolean result = orderService.confirmReceipt(model.getOrderNo());
        if (result) {
            return ResponseResult.createBySuccess("收货成功");
        }
        return ResponseResult.createByError("该订单状态不能收货");
    }

    /**
     * 订单数量
     */
    @PostMapping("/get_order_num")
    public ResponseResult<JSONObject> getOrderNum() {
        Integer num = orderService.getOrderNum();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("num", num);
        return ResponseResult.createBySuccess(jsonObject);
    }
}
